<template>
  <div>
    <div  style="position: relative;">
      <i class="el-icon-close" @click="closeDialogTop" style="position:absolute;cursor: pointer; font-size:22px; top: -48px; right: 0px"></i>
    </div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="baseInfo"
      :list="initFormBase"
      :cols="3"
      :labelWidth="140"
      :isSubmitBtn="false"
      :formType="infoBasicForm"
      @formChange="formChangeHandleBase"
    />
    <BreakSpace label="授权信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="empowerInfo"
      :list="initFormEmpower"
      :cols="3"
      :labelWidth="140"
      :isSubmitBtn="false"
      :formType="infoBasicForm"
      @formChange="formChangeHandleEmpower"
    />
    <div class="dialogBottmView" >
      <el-button @click="closeDialogTop">取 消</el-button>
	  <!-- 不校验保存 -->
      <el-button @click="saveDialog" :loading="saveLoading" v-if="infoBasic != 'show' && !isCreateTempAccount" :disabled="isSave" type="primary">保存</el-button>
	  <!-- 校验保存 -->
      <el-button @click="saveCompleteDialog" :loading="saveLoading" v-if="infoBasic != 'show' && isCreateTempAccount" :disabled="isSave" type="primary">保存</el-button>

      <el-button @click="createDialog" :loading="saveLoading" v-if="infoBasic != 'show'" :disabled="isCreateTempAccount" type="primary">生成临时账号</el-button>
      <el-button @click="turnDialog" :loading="saveLoading" v-if="infoBasic != 'show'" :disabled="isChangeDealer" type="primary">转经销商</el-button>
    </div>
  </div>
</template>
<script>
// import personnelMadal from '@/pages/cms-common/user'
import { dictionary } from '@/utils/dictMixin';
import { insertDealerApply,updataDealerApply ,getCurrentUser,createDealer,turnDealer} from '@/api/cms-api/origin/construct/dealer';
import { getRegion, getCommunity } from '@/api/cms-common/dealerdata/index';
import { region } from '@/api/cms-common/region';
import * as types from '@/api/cms-api/origin/construct/dealer/types';
import { getAppId } from '@/assets/js/auth';//获取登录用户
import { dealerCommon,isArcfox } from '@/api/cms-api/origin/commonDealerType';		//渠道类型工具类
import { formatdateByDay } from '@/utils';

import * as utils from '@/utils/utils'	//工具类

export default {
  components: {

  },
  props: {
    value:{
        type: Object,
        default: null
    }
  },
  mixins: [dictionary,region,dealerCommon],
  data() {
    return {
      applyId:"-1",  //经销商申请ID，修改时进入赋值
      formData:[],
      dealerList:[],
      productList:[],
      dataSource:[],
      isSelectColumn: false,
      infoBasic:this.value.type,  //生成临时账号与转正式经销商按钮是否显示参数
      infoBasicForm:this.value.type,  //表单是否可以编辑参数
      initFormBase: this.initFormBaseFun(),
      initFormEmpower: this.initFormEmpowerFun(),
      submitType:'',  //提交类型 生成临时账号 create 转经销商 turn, 校验并保存:completeSave
      isSave:false, //保存按钮是否可用
      isCreateTempAccount:false, //生成临时账号按钮是否可用
      isChangeDealer:false, //转经销商按钮是否可用
      cityData: {},
      saveLoading: false,
      dealerTypeRemove:[] //渠道类型排除内容
    };
  },
  mounted() {
    //如果传入参数不为空，则说明是修改进入，初始化对应数据
    if(this.value!=null && (this.value.type=="edit"||this.value.type=="show")){
      this.init();
    }
  },
  created() {
    //初始化事业部下拉数据
    this.getDeptData();

    //初始化省份信息
    this.initProvince();

    //初始化品牌信息
    this.initBrand();
  },
  methods: {
    //修改时，初始化页面数据
    init(){
      debugger
      //初始化所在市，区
      if(this.value.rowData['provinceId']){
        this.initCity(this.value.rowData['provinceId'],false);
      }
      if(this.value.rowData['cityId']){
        this.initTown(this.value.rowData['cityId'],false);
      }
      //初始化办事处
      if(this.value.rowData['deptId']){
        this.changeDeptSelect(this.value.rowData['deptId'],false);
      }
      //初始化渠道类型
      if(this.value.rowData['dealerType']){
        this.changeDealerType(this.value.rowData['dealerType'],false);
      }

      // 更新接口需要的记录 ID
      this.applyId = this.value.rowData.applyId;
      //判断是否已生成临时账号
      if(this.value.rowData.isCreateTempAccount=="10041001"){
        this.isCreateTempAccount = true;
      }
      //判断是否已转经销商
      if(this.value.rowData.isChangeDealer=="10041001"){
        this.isChangeDealer = true;
        this.isCreateTempAccount = true;
        this.isSave = true;
        this.infoBasicForm='show';
      }
      //循环为表单赋值
      this.initFormBase.forEach(x => {
        if(x.fieldName === 'ownBrandCodeList'){
          if(this.value.rowData['ownBrandCode']){
            let list = this.value.rowData['ownBrandCode'].split(',');
            x.initialValue  = list.map(x => parseInt(x));
          }else{
            x.initialValue  = [];
          }
        }else if(x.fieldName === 'createdAt'){
          if(this.value.rowData[x.fieldName]){
             x.initialValue = formatdateByDay(this.value.rowData[x.fieldName]);
          }
        }else{
          if(this.value.rowData[x.fieldName]){
            x.initialValue = this.value.rowData[x.fieldName];
          }
        }

        if(x.fieldName=='appId'){
          
          //判断品牌，如果为ARCFOX时，细分类别显示且必填,渠道类型中，包含直营店、非直营店
          if(this.isArcfox()){
            this.dealerTypeRemove = ['25381006'];
          }else{
            this.dealerTypeRemove = ['25381004','25381005','25381006'];//2538
          }
          this.initFormBase[4].itemList = this.createDictListRemoveCodes('2538',this.dealerTypeRemove);
        }
      });

      this.initFormEmpower.forEach(x => {
        if(this.value.rowData[x.fieldName]){
          x.initialValue = this.value.rowData[x.fieldName];
        }
      });

      //如果已经转经销商，则设置页面字段不可填写   
      if(this.infoBasicForm=='show' || this.isChangeDealer){
          this.initFormBase.find(item => item.fieldName === 'ownBrandCodeList').disabled = true;
          this.initFormBase.find(item => item.fieldName === 'salesLevel').disabled = true;
          this.initFormBase.find(item => item.fieldName === 'serviceLevel').disabled = true;
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').disabled = true;
      }
      
    },
    //初始化品牌信息
    async initBrand(){
      //查询当前登录用户信息
      let res = await getCurrentUser({});
      if (res.resultCode === 200) {
        this.initFormBase[0].initialValue=res.data.appId.toUpperCase();

        //判断品牌，如果为ARCFOX时，细分类别显示且必填,渠道类型中，包含直营店、非直营店
        if(this.isArcfox()){
          this.dealerTypeRemove = ['25381006'];
        }else{
          this.dealerTypeRemove = ['25381004','25381005','25381006'];//2538
        }
        this.initFormBase[4].itemList = this.createDictListRemoveCodes('2538',this.dealerTypeRemove);
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'获取品牌信息错误!'});
      }
    },
    //基本信息数据
    initFormBaseFun() {
      let form1 = [{//0
          type: 'INPUT',
          label: '品牌',
          fieldName: 'brandIdTmp',
          // fieldName: 'appId',
          placeholder: '请输入',
          initialValue: '',
          disabled:true
        },
        {//1
          type: 'INPUT',
          label: '经销商简称',
          fieldName: 'dealerShortname',
          placeholder: '请输入',
          initialValue: '',
          maxlength:90,
          rules: isArcfox() ?
          [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 15,
              message: '长度最大15字符',
              trigger: 'blur'
            }
          ]:
          [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 8,
              message: '长度最大8字符',
              trigger: 'blur'
            }
          ]
        },
        {//2
          type: 'INPUT',
          label: '临时代码',
          fieldName: 'dealerCode',
          placeholder: '系统自动生成',
          // initialValue: '系统自动生成',
          disabled:true
        },
        {//3
          type: 'INPUT',
          label: '公司全称',
          fieldName: 'dealerName',
          placeholder: '请输入',
          initialValue: '',
          selfCols:3,
          maxlength:300,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 300,
              message: '长度最大300字符',
              trigger: 'blur'
            }
          ]
        },
        {//4
          type: 'SELECT',
          label: '渠道类型',
          fieldName: 'dealerType',
          placeholder: '请选择',
          initialValue: '',
          itemList:[],
          change: val => {
            this.changeDealerType(val);
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },];

      // let form2_1 = [{//5
      //     type: 'SELECT',
      //     label: '细分类型',
      //     fieldName: 'subType',
      //     placeholder: '请选择',
      //     initialValue: '',
      //     itemList: this.createDictList('2539'),
      //     rules: [],
      //     rulesTmp: [
      //         {   required: true,
      //             message: '请输入',
      //             trigger: 'change'
      //         }
      //     ]
      //   },];
      // let form2_2 = [{//5
      //     type: 'SELECT',
      //     label: '细分类型',
      //     fieldName: 'subType',
      //     placeholder: '请选择',
      //     initialValue: '',
      //     itemList: this.createDictList('2539'),
      //     rules: [
      //       {   required: true,
      //             message: '请输入',
      //             trigger: 'change'
      //         }
      //     ],
      //     rulesTmp: [
      //         {   required: true,
      //             message: '请输入',
      //             trigger: 'change'
      //         }
      //     ]
      //   },];

      let form3 = [{//6
          type: 'SELECT',
          label: '经销商级别',
          fieldName: 'shopLevel',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2540'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//7
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: val => {
            this.changeDeptSelect(val);
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//8
          type: 'SELECT',
          label: '办事处',
          fieldName: 'officeId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//9
          type: 'SELECT',
          label: '建设类型',
          fieldName: 'constructType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2541'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//10
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          initialValue: "",
          change: (value) => {
            this.initCity(value)
          },
          itemList:  [],
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {//11
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          initialValue: "",
          itemList: [],
          change: (value) => {
            this.initTown(value)
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//12
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          initialValue: "",
          itemList:  [],
          change: (value) => {
            this.initDistrict(value)
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//22
          type: 'INPUT',
          label: '建设地址',
          fieldName: 'constructAddress',
          placeholder: '请输入',
          initialValue: '',
          maxlength:300,
          selfCols:3,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 300,
              message: '长度最大300字符',
              trigger: 'blur'
            }
          ]
        },
        {//13
          type: 'INPUT',
          label: '店端主要联系人',
          fieldName: 'dealerLinkman',
          placeholder: '请输入',
          initialValue: '',
          maxlength:100,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 100,
              message: '长度最大100字符',
              trigger: 'blur'
            }
          ]
        },
        {//14
          type: 'INPUT',
          label: '联系人电话',
          fieldName: 'dealerPhone',
          placeholder: '请输入',
          initialValue: '',
          maxlength:11,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { 
              pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/, 
              message: '请填写正确的手机格式', 
              trigger: 'blur' 
            }
          ]
        },
        {//15
          type: 'INPUT',
          label: '店端联系人邮箱',
          fieldName: 'dealerEmail',
          placeholder: '请输入',
          initialValue: '',
          maxlength:60,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 60,
              message: '长度最大60字符',
              trigger: 'blur'
            },
            {
              type:'email',
              message: '请输入正确的邮箱',
              trigger: 'blur'
            }
          ]
        },
        {//16
          type: 'INPUT',
          label: '厂家负责人',
          fieldName: 'oemLinkman',
          placeholder: '请输入',
          initialValue: '',
          maxlength:100,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            { max: 100,
              message: '长度最大100字符',
              trigger: 'blur'
            }
          ]
        },
        {//17
          type: 'INPUT',
          label: '负责人电话',
          fieldName: 'oemPhone',
          placeholder: '请输入',
          initialValue: '',
          maxlength:11,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            ,
            { 
              pattern: /^[1][3,4,5,7,8][0-9]{9}$/, 
              message: '请填写正确的手机格式', 
              trigger: 'blur' 
            }
          ]
        },
        { type: 'MULTIPLE_SELECT',
          label: '体系内品牌',
          fieldName: 'ownBrandCodeList',
          placeholder: '请选择',
          initialValue: [],
          itemList: this.createDictList('2545'),},
        
        {//19
          type: 'INPUT',
          label: '项目设计师',
          fieldName: 'projectDesigner',
          placeholder: '请输入',
          initialValue: '',
          maxlength:100,
          
        },
        {//20
          type: 'INPUT',
          label: '设计师电话',
          fieldName: 'designerPhone',
          placeholder: '请输入',
          initialValue: '',
          maxlength:11,
          rules: [
            
            { 
              pattern: /^[1][3,4,5,7,8][0-9]{9}$/, 
              message: '请填写正确的手机格式', 
              trigger: 'blur' 
            }
          ]
        },
        {//18
          type: 'SELECT',
          label: '展厅级别',
          fieldName: 'salesLevel',
          placeholder: '请选择',
          initialValue: '',
          hidden:false,
          itemList: this.createDictList('2542')
        },
        {//21
          type: 'SELECT',
          label: '售后级别',
          fieldName: 'serviceLevel',
          placeholder: '请选择',
          initialValue: '',
          hidden:false,
          itemList: this.createDictList('2543')
        },
        
        // {//23
        //   type: 'SELECT',
        //   label: '体系内品牌',
        //   fieldName: 'ownBrandCode',
        //   placeholder: '请选择',
        //   initialValue: '',
        //   itemList: this.createDictList('2545')
        // }
        {
          type: 'INPUT',
          label: '创建人',
          fieldName: 'createdByName',
          placeholder: '',
          initialValue: '',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '创建时间',
          fieldName: 'createdAt',
          placeholder: '',
          initialValue: '',
          disabled:true,
        },

      ];

      //细分类别：品牌为ARCFOX时显示且必填；
      // if(this.isArcfox()){
      //   return form1.concat(form2_2).concat(form3);
      // }else{
        return form1.concat(form3);
      // }

    },
    //授权信息数据
    initFormEmpowerFun() {
      return [
         {
          type: 'SELECT',
          label: '是否边建边销',
          fieldName: 'isSaling',
          placeholder: '请选择',
          initialValue: '',
          hidden:false,
          itemList: this.createDictList('1004'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '是否边建边修',
          fieldName: 'isServicing',
          placeholder: '请选择',
          initialValue: '',
          hidden:false,
          itemList: this.createDictList('1004'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        // {
        //   type: 'INPUT_NUMBER',
        //   label: '临时工时',
        //   fieldName: 'tempLabour',
        //   placeholder: '请输入',
        //   initialValue: 0,
        //   precision:2,
        //   max:9999.99
         // }
         {type:'INPUT',label:'临时工时系数',fieldName:'tempLabour',initialValue:'',placeholder: '请输入',maxlength:4,hidden:false,
            unitRender:() => {
               return<span>%</span>
            },
            change: val => {
               utils.toNumber(this.initFormEmpower,"tempLabour",val);
            },
         }
        
      ];
    },
    
    //渠道类型切换
    changeDealerType(type,flag=true) {
      if (type) {
        //如果为4s店 展厅级别和售后级别都显示 经销商级别显示
        if(type === types.DEALER_TYPE_4S){
          this.$nextTick(() =>{
            if(flag){
              this.initFormBase.find(item => item.fieldName === 'salesLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'serviceLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'shopLevel').initialValue = undefined;
            }
            this.initFormBase.find(item => item.fieldName === 'salesLevel').hidden = false;
            this.initFormBase.find(item => item.fieldName === 'serviceLevel').hidden = false;
            this.initFormBase.find(item => item.fieldName === 'shopLevel').hidden = false;
          } )
          //是否边建边销和是否边建边修显示并必填
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').hidden = false;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').hidden = false;
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').initialValue = '';
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').hidden = false;


        //如果为单一销售 展厅级别显示，售后级别隐藏  经销商级别显示
        }else if(type === types.DEALER_TYPE_SINGLE_SALE){
          this.$nextTick(() =>{
            if(flag){
              this.initFormBase.find(item => item.fieldName === 'salesLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'serviceLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'shopLevel').initialValue = undefined;
            }
            this.initFormBase.find(item => item.fieldName === 'salesLevel').hidden = false;
            this.initFormBase.find(item => item.fieldName === 'serviceLevel').hidden = true;
            this.initFormBase.find(item => item.fieldName === 'shopLevel').hidden = false;
          } )
          //是否边建边销显示并必填 ,是否边建边修不显示 临时工时不显示
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').hidden = false;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').hidden = true;
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').initialValue = '';
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').hidden = true;


        //如果为单一售后  售后级别显示，展厅级别隐藏 经销商级别显示
        }else if(type === types.DEALER_TYPE_SINGLE_AFTER){
          this.$nextTick(() =>{
            if(flag){
              this.initFormBase.find(item => item.fieldName === 'salesLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'serviceLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'shopLevel').initialValue = undefined;
            }
            this.initFormBase.find(item => item.fieldName === 'salesLevel').hidden = true;
            this.initFormBase.find(item => item.fieldName === 'serviceLevel').hidden = false;
            this.initFormBase.find(item => item.fieldName === 'shopLevel').hidden = false;
          } )
          //是否边建边销不显示 ,是否边建边修显示并必填
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').hidden = true;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').hidden = false;
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').initialValue = '';
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').hidden = false;

        //如果为SPACE和RACE  经销商级别、展厅级别、售后级别字段不显示
        }else if(type === types.DEALER_TYPE_DIRECT_SHOP || type === types.DEALER_TYPE_NOT_DIRECT_SHOP){
          this.$nextTick(() =>{
            if(flag){
              this.initFormBase.find(item => item.fieldName === 'salesLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'serviceLevel').initialValue = undefined;
              this.initFormBase.find(item => item.fieldName === 'shopLevel').initialValue = undefined;
            }
            this.initFormBase.find(item => item.fieldName === 'salesLevel').hidden = true;
            this.initFormBase.find(item => item.fieldName === 'serviceLevel').hidden = true;
            this.initFormBase.find(item => item.fieldName === 'shopLevel').hidden = true;
          } )
          //边建边销要保留,是否边建边修、临时工时隐藏
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isSaling').hidden = false;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').initialValue = undefined;
          this.initFormEmpower.find(item => item.fieldName === 'isServicing').hidden = true;
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').initialValue = '';
          this.initFormEmpower.find(item => item.fieldName === 'tempLabour').hidden = true;

        }
      }
    },
    //获取事业部下拉列表数据
    async getDeptData() {
      const res = await this._$getNotBigcustomerData();
      //if (res.resultCode === 200) {
        //this.initFormBase.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
      //}
      this.initFormBase.find(item => item.fieldName === 'deptId').itemList = res;
    },

    //事业部下拉改变执行方法，级联查询该事业部下的办事处
    async changeDeptSelect(deptId,flag = true) {
      if(flag){
        this.initFormBase.find(item => item.fieldName === 'officeId').initialValue = '';
        this.initFormBase.find(item => item.fieldName === 'officeId').itemList = [];
      }
      if (!deptId) {
      } else {
        const res = await getCommunity(deptId);
        if (res.resultCode === 200) {
         this.initFormBase.find(item => item.fieldName === 'officeId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
        }
      }
    },

    //初始化所在省份
    async initProvince(){
      let pl = this.createRegionList();
      this.initFormBase.find(item => item.fieldName === 'provinceId').itemList = pl;
      // this.initFormBase[19].itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId,flag = true){
      if(flag){
        //清空市
        this.initFormBase.find(item => item.fieldName === 'cityId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'cityId').itemList = [];
        //清空县
        this.initFormBase.find(item => item.fieldName === 'districtId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'districtId').itemList=[];
        //动态填入建设地址
        if(provinceId){
          this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue = this.createRegionText(provinceId);
        }else{
          this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue = "";
        }
      }

      if(!provinceId || provinceId==null || typeof(provinceId)=='undefined'){
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      this.initFormBase.find(item => item.fieldName === 'cityId').itemList = cl;
    },
    //初始化所在县数据
    async initTown(cityId,flag = true){
      
      if(flag){
        //清空县
        this.initFormBase.find(item => item.fieldName === 'districtId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'districtId').itemList=[];
        //动态填入建设地址
        if(cityId){ //如果所选的市不为空
          this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue =this.createRegionText(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue) + this.createRegionText(cityId);
        }else{  //如果所选的市为空
          if(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue){ //如果所选的省不为空
            this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue =this.createRegionText(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue);
          }else{
            this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue = "";
          }
        }
      }
      if(!cityId || cityId==null || typeof(cityId)=='undefined'){
        return;
      }
      let td = this.cityData[cityId].children;
      let tl = this.createSubRegionList(td);
      //部分城市为县级市，没有县区的下级数据
      
      if(tl.length==0){
        this.initFormBase.find(item => item.fieldName === 'districtId').rules[0].required=false;
        this.$refs.baseInfo.$refs.form.clearValidate();
      }else{
        this.initFormBase.find(item => item.fieldName === 'districtId').rules[0].required=true;
        this.initFormBase.find(item => item.fieldName === 'districtId').itemList = tl;
      }

    },
    initDistrict(districtId){
      //动态填入建设地址
      if(districtId){ //如果所选的县不为空
        this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue =this.createRegionText(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue) + this.createRegionText(this.initFormBase.find(item => item.fieldName === 'cityId').initialValue) + this.createRegionText(districtId);
      }else{  //如果所选的县为空
        if(this.createRegionText(this.initFormBase.find(item => item.fieldName === 'cityId').initialValue)){ //如果所选的市不为空
          this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue =this.createRegionText(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue) + this.createRegionText(this.initFormBase.find(item => item.fieldName === 'cityId').initialValue);
        }else{  //如果所选的市为空
          if(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue){ //如果所选的省不为空
            this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue =this.createRegionText(this.initFormBase.find(item => item.fieldName === 'provinceId').initialValue);
          }else{  //如果所选的省为空
            this.initFormBase.find(item => item.fieldName === 'constructAddress').initialValue = "";
          }
        }
      }
        
    },
    // 基础信息表单数据获取
    formChangeHandleBase(data){
      this.formData.base = data;
      this.formData.base.ownBrandCode = data.ownBrandCodeList.join(",");
      this.$refs.empowerInfo.SUBMIT_FORM();
    },
    // 授权信息表单数据获取
    formChangeHandleEmpower(data){
      this.formData.empower = data;
      //创建临时账号
      if(this.submitType == 'create'){
        this.doCreateDialog();
      }else if(this.submitType == 'turn'){  //转正式经销商
        this.doTurnDialog();
      }else if(this.submitType === 'completeSave'){//如果是已经创建临时账号后，则需要必填的保存
        this.doCompleteSaveDialog();
	  }
    },
    //保存
    async saveDialog(){
      this.saveLoading = true;
      this.$refs.baseInfo.excuteFormData(this.$refs.baseInfo.form);
      let base = Object.assign({},this.$refs.baseInfo.form);
      if(base.ownBrandCodeList){
        base.ownBrandCode = base.ownBrandCodeList.join(",");
      }
      this.$refs.empowerInfo.excuteFormData(this.$refs.empowerInfo.form);
      let empower = this.$refs.empowerInfo.form;

      let submitData = Object.assign({},this.value.rowData,base,empower);
      console.log("submitData======",submitData);
      let res;
      //如果传入参数不为空并且为修改
      if(this.value!=null && this.value.type=="edit"){
        res = await updataDealerApply(submitData,this.applyId);
      }else{
        res = await insertDealerApply(submitData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        // 重新加载
        this.closeDialog('refresh');
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        this.saveLoading = false;
        //this.closeDialog();
      }

    },
    async saveCompleteDialog(){
      this.submitType = 'completeSave';
      this.$refs.baseInfo.SUBMIT_FORM();
    },
    //生成临时账号
    createDialog(){
      this.submitType = 'create';
      this.$refs.baseInfo.SUBMIT_FORM();
    },
    //转经销商
    turnDialog(){
      this.submitType = 'turn';
      this.$refs.baseInfo.SUBMIT_FORM();
    },
     //执行生成临时账号
    async doCreateDialog(){
        this.saveLoading = true;
        let submitData = Object.assign({},this.value.rowData,this.formData.base,this.formData.empower);
        console.log("submitData:",submitData);
        let res;

        res = await createDealer(submitData,this.applyId);
        if (res.resultCode === 200) {
          this.$notify.success({title: '提示信息',message:'生成临时账号成功!'});
          // 重新加载
          this.closeDialog('refresh');
        }else {
          this.$notify.error({title: '提示信息',message:res.errMsg||'生成临时账号失败!'});
          this.saveLoading = false;
          //this.closeDialog();
        }

    },
    //执行转经销商
    async doTurnDialog(){
        this.saveLoading = true;
        let submitData = Object.assign({},this.value.rowData,this.formData.base,this.formData.empower);
        console.log("submitData:",submitData);
        let res;

        res = await turnDealer(submitData,this.applyId);
        if (res.resultCode === 200) {
          this.$notify.success({title: '提示信息',message:'转经销商成功!'});
          // 重新加载
          this.closeDialog('refresh');
        }else {
          this.$notify.error({title: '提示信息',message:res.errMsg||'转经销商失败!'});
          this.saveLoading = false;
          //this.closeDialog();
        }
	},
	//校验并保存
    async doCompleteSaveDialog(){
      this.saveLoading = true;
	  let submitData = Object.assign({},this.value.rowData,this.formData.base,this.formData.empower);
	  let res;
      //如果传入参数不为空并且为修改
      if(this.value!=null && this.value.type=="edit"){
        res = await updataDealerApply(submitData,this.applyId);
      }else{
        res = await insertDealerApply(submitData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        // 重新加载
        this.closeDialog('refresh');
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        this.saveLoading = false;
	  }
    },
    closeDialogTop(){
      this.$confirm('确认关闭窗口吗？您所做的操作将不会被保存！', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
      })
      .then(() => {
        this.closeDialog('closeDialog');
      })
    },
    //取消操作
    closeDialog(type) {
     this.$emit('close', { visible: false,type:type});
    }
  }
};
</script>

<style lang="less" scoped>
</style>
